// components/popup-location-condition/popup-location-condition.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    isPopupVisible: {
      type: Boolean,
      value: false
    },
    licensePlateNum: {
      type: String,
      value: ""
    },
    startTime: {
      type: String,
      value: ""
    },
    endTime: {
      type: String,
      value: ""
    },
    duration: {
      type: Number,
      value: 500
    },
    minute: {
      type: Number,
      value: 10
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    radioOptions: [{
        label: '慢速',
        value: '1000'
      },
      {
        label: '正常',
        value: '500',
        checked: true
      },
      {
        label: '快速',
        value: '200'
      },
      {
        label: '最快',
        value: '50'
      }
    ],
    minuteRange: [10, 20, 30, 40, 50],
    minuteIndex: 0
  },
  observers: {
    'isPopupVisible': function (selectIndex) {
      // 在 rate被设置时，执行这个函数
      console.log(this.data.isPopupVisible)
      if (this.data.isPopupVisible == true) {
        this.data.radioOptions.forEach(item => {
          if (item.value === this.data.duration) {
            item.checked = true;
          } else {
            item.checked = false;
          }
        });
        this.data.minuteRange.forEach((item, index) => {
          if (item === this.data.minute) {
            this.data.minuteIndex = index;
          }
        });
      }
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    download: function () {
      this.setData({
        isPopupVisible: false
      })
      this.triggerEvent('download', {
        startTime: this.data.startTime,
        endTime: this.data.endTime,
        duration: this.data.duration,
        minute: this.data.minute
      });
    },
    closePopup: function () {
      this.setData({
        isPopupVisible: false
      })
    },
    handleStartTimeChange: function (e) {
      this.setData({
        startTime: e.detail.date
      })
    },
    handleEndTimeChange: function (e) {
      this.setData({
        endTime: e.detail.date
      })
    },
    radioChange(e) {
      console.log('radio发生change事件，携带value值为：', e.detail.value)
      const items = this.data.radioOptions
      for (let i = 0, len = items.length; i < len; ++i) {
        items[i].checked = items[i].value === e.detail.value
      }

      this.setData({
        radioOptions: items,
        duration: e.detail.value
      })
    },
    bindPickerChange: function (e) {
      this.setData({
        minuteIndex: e.detail.value,
        minute: this.data.minuteRange[e.detail.value]
      })
    }
  }
})